package org.gntc.prayer;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.BaseColumns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class PryaerListAdapter extends BaseAdapter
{
	protected Cursor c;
	protected LayoutInflater mInflater;
	protected Bitmap mIconJesus;
	protected Bitmap mIconVision;
	protected boolean mbChanged = false;

	public PryaerListAdapter (Context context, Cursor cursor)
	{
		super();
		// TODO Auto-generated constructor stub
		c = cursor;
		mInflater = LayoutInflater.from(context);
		
		mIconJesus = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_jesus);
		mIconVision = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_vision);
	}

	@Override
	public int getCount ()
	{
		// TODO Auto-generated method stub
		return c.getCount();
	}

	@Override
	public Object getItem (int position)
	{
		// TODO Auto-generated method stub
		if(c.moveToPosition(position))
			return c;
		else
			return null;
	}

	@Override
	public long getItemId (int position)
	{
		// TODO Auto-generated method stub
		if(c.moveToPosition(position))
			return c.getInt(c.getColumnIndex(BaseColumns._ID));
		else
			return 0;
	}

	@Override
	public View getView (int position, View convertView, ViewGroup parent)
	{
		// TODO Auto-generated method stub
		ViewHolder holder;
		
		if (convertView == null)
		{
			convertView = mInflater.inflate(R.layout.list_item, null);
			
			holder = new ViewHolder();
			holder.icon = (ImageView)convertView.findViewById(R.id.ic_category);
			holder.title = (TextView)convertView.findViewById(R.id.title);
			holder.favorite = (CheckBox)convertView.findViewById(R.id.cb_fovorite);
			holder.selected = (CheckBox)convertView.findViewById(R.id.cb_selected);
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder)convertView.getTag();
		}
		
		if (mbChanged)
		{
			holder.icon.setImageBitmap(mIconJesus);
			mbChanged = false;
		}
		else
		{
			holder.icon.setImageBitmap(mIconVision);
			mbChanged = true;
		}
		
		holder.title.setText(R.string.app_name);
		holder.favorite.setChecked(true);
		holder.selected.setChecked(false);
		
		return convertView;
	}
	
	public class ViewHolder {
		ImageView icon;
		TextView title;
		CheckBox favorite;
		CheckBox selected;
	}
}
